/**
 * 
 */
package sailing.ui.treecell;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sailing.resource.Resource;

import cjlite.logger.Logger;

/**
 * @author kevin
 * 
 */
public class SubjectTreeCell extends JPanel {

	private static ImageIcon folderIcon = Resource.getImageIcon("/ui/tree/tree_folder.png");
	private static ImageIcon leafIocn = Resource.getImageIcon("/ui/tree/tree_leaf.png");

	private static Color selectBgColor = new Color(138, 197, 255);
	private static Color selectBgColor2 = new Color(176, 216, 255);

	private static Logger logger = Logger.thisClass();

	public SubjectTreeCell(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean addListener) {
		super();
		Box cell = Box.createHorizontalBox();
		cell.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 7));
		JLabel label = new JLabel();
		// label.set
		if (leaf) {
			label.setIcon(leafIocn);
		} else {
			label.setIcon(folderIcon);
		}

		label.setText(value.toString());

		cell.add(label);
		cell.add(Box.createHorizontalStrut(40));
		JButton editBtn = new JButton("编辑");
		cell.add(editBtn);
		cell.add(Box.createHorizontalStrut(20));
		JButton deleteBtn = new JButton("删除");
		cell.add(deleteBtn);

		if (addListener) {
			editBtn.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					logger.trace("edit btn click");
				}
			});
			deleteBtn.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					logger.trace("delete btn click");
				}
			});
		}

		if (selected) {
			cell.setOpaque(true);
			cell.setBackground(selectBgColor2);
		}
		this.setLayout(new BorderLayout());
		this.add(cell, BorderLayout.CENTER);
	}
	
	private ActionListener editAl;

	public void addEditBtnListener(ActionListener editAl) {
		this.editAl = editAl;
	}

	private ActionListener deleteAl;

	public void addDeleteBtnListener(ActionListener deleteAl) {
		this.deleteAl = deleteAl;
	}

}
